##
# Show status of MySQL
# @param $1 install home
# @param $2 sql statements
# @param $3 delimiter
# @param $4 host
# @param $5 port
# @param $6 username
# @param $7 password
#
use strict;
my ($MYSQL_HOME,$SQL,$DELIM,$HOST,$PORT,$USER,$PASS) = @ARGV;
my $PARAM="";
$PARAM="$PARAM -h\"$HOST\"" if (defined($HOST) && $HOST ne "");
$PARAM="$PARAM -P\"$PORT\"" if (defined($PORT) && $PORT ne "");
$PARAM="$PARAM -u\"$USER\"" if (defined($USER) && $USER ne "");
$PARAM="$PARAM -p\"$PASS\"" if (defined($PASS) && $PASS ne "");
my $MYSQL="$MYSQL_HOME/bin/mysql";
my $MYSQLADMIN="$MYSQL_HOME/bin/mysqladmin";
if ($^O =~ m/MSWin32/) {
  $MYSQL =~ s/\//\\/g;
  $MYSQLADMIN =~ s/\//\\/g;
}
# Check if mysqld is alive
my $retmsg=`$MYSQLADMIN $PARAM ping 2>&1`;
chomp($retmsg);
if ( "$retmsg" ne "mysqld is alive" ) {
    print "em_result=Status${DELIM}Down\n";
    exit 0;
}
#print "em_result=Status${DELIM}Up\n";
# Execute command and display the result as key-value pairs
my @KV=`$MYSQL -N -s $PARAM -e"$SQL"`;
chomp(@KV);
my %hash = ();
for (@KV) {
    my ($key, $value) = split /\t/, $_;
    $hash{$key} = $value;
}
while (my ($key, $value) = each(%hash)) {
    print "em_result=${key}${DELIM}${value}\n";
}
exit 0;

